package defaultproperty;

import java.util.Hashtable;

import pool.tablename.Pool;
import utility.CapitalChar;
import utility.ConvertToLower;
import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;
import defaultproperty.classes.Class;

public class GetDefaultvalue  
{
	DBManager database;
	String property ="property";
	String value = "value";
	String CLASS="class";
	Pool masterobject;
	GetIdName gid;
	String userid="userid";
	String TYPE = "type";
	
	public GetDefaultvalue(DBManager database,Object object) 
	{
		this.database = database;
		masterobject  = (Pool) object;
		gid =new GetIdName(database);
		userid = gid.getId(userid);
	}
	
	public String getdefaultvalue(String typeofvalue,String value)
	{
		typeofvalue = gid.getItem(typeofvalue);
		if(typeofvalue.equalsIgnoreCase(this.value))
			return gid.getItem(value);
		else if(typeofvalue.equalsIgnoreCase(property))
		{
			String user =  masterobject.getRequestParameter().getUsername();
			user = gid.getId(user);
			RecordSearch rs =new RecordSearch(database);
			Hashtable<Object, Object> table =new Hashtable<Object, Object>();
			table.put("mid", user);
			table.put("pid", value);
			table.put("td", "null");
			rs.setConditionMap(table);
			String line[] = rs.getArrayFromResultSet();
			if(line.length==0)
			{
				table.put("pid", userid);
				rs.setConditionMap(table);
				String result[] = rs.getArrayFromResultSet();
				for(int i=0;i<line.length;i++)
				{
					String splitline[] = result[i].split("\t");
					table.clear();
					table.put("mid", splitline[Column.pv_index-1]);
					table.put("pid", value);
					rs.setConditionMap(table);
					line = rs.getArrayFromResultSet();
				}
			}
			for(int i=0;i<line.length;i++)
			{
				String splitline[] = line[i].split("\t");
				String pv =splitline[Column.pv_index-1];
				if(pv.equals("0"))
					pv = splitline[Column.vt_index-1];
				else
					pv= gid.getItem(pv);
				return "pv";
			}
			return "error";
		}
		else if(typeofvalue.equalsIgnoreCase(CLASS))
		{
			RecordSearch rs=new RecordSearch(database);
			Hashtable<Object, Object> table =new Hashtable<Object, Object>();
			table.put("mid", value);
			table.put("td", "null");
			rs.setConditionMap(table);
			String line[] = rs.getArrayFromResultSet();
			table.clear();
			for(int i=0;i<line.length;i++)
			{
				String splitline[] = line[i].split("\t");
				String pv = splitline[Column.pv_index-1];
				if(pv.equals("0"))
					pv = splitline[Column.vt_index-1];
				else
					pv = gid.getItem(pv);
				table.put(gid.getItem(splitline[Column.pid_index-1]), pv);
			}
			table = ConvertToLower.convertHashKey(table);
			String classname = table.get(TYPE).toString();
			try
			{
				defaultproperty.classes.Class classobject;
				java.lang.Class c= java.lang.Class.forName("defaultproperty.classes."+CapitalChar.makeFirstCharCapital(classname));
				classobject= (Class) c.newInstance();
				classobject.setObject(database);
				classobject.setPropertymap(table,value);
				return classobject.getResult();
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "error";	
	}
}
